function reset(){
	$("#frm_login")[0].reset();
}
function r_reset(){
	$("#frm_register")[0].reset();
}
function register(){
	var uname=$("#r_uname").val();
	if(uname.length==0){
		layer.msg("用户名不能为空！",{
			icon:2,
			time:1000
		});
		return;
	}
	
	var upwd=$("#r_upwd").val();
	var patrn=/^[A-Za-z0-9._]{6,12}$/
	if(!patrn.exec(upwd)){
		layer.alert("请输入合法的密码！",{
		    icon:3,
			btnClose:true
		});
			return;
	}
	
	var cupwd=$("#r_cupwd").val();
	/*var patrn=/^[A-Za-z0-9._]{6,12}$/
	if(!patrn.exec(upwd)){
		layer.alert("请输入合法的密码！","#upwd",{
		    icon:3,
			btnClose:true
		});
			return;
	}*/
	if(cupwd.length==0){
		layer.msg("确认密码不能为空！",{
			icon:2,
			time:1000
		});
		return;
	}
	
	if(upwd!=cupwd){
		layer.tips("两次密码不一致！","#r_cupwd",{
			tips:[2,"#000000"]
		});
		    return;
	}
	
	var email=$("#r_email").val();
	var reg=/^[A-Za-z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
	if(!reg.test(email)){
		layer.tips("邮箱输入有误！","#r_email",{
			tips:[2]
		});
		    return;
	}
	//ajax 根据json的结果，跳到登录url 
	jQuery.ajax({
		url:"js/register.json",
		date:{},
		type:"get",
		dataType:"json",
		success:function(result){
			var temp=result.code;
			if(temp==1){
				window.location.href="login.html"
			}else{
				layer.msg("注册失败！",{
					icon:2
				});
			}
		},
		error:function(){
			layer.msg("访问服务器失败！",{
				icon:2
			});
		}
	});
	
	/*window.location.href="login.html";*/

}


jQuery(function($) {

	var code = drawPic();
	document.getElementById("vcode_img").onclick = function(e) {
		e.preventDefault();
		code = drawPic();
	}

	$("#login").click(function(event) {
		var uname = $('#uname').val();
		var upwd = $('#upwd').val();
		var vcode= $('#vcode').val();
		//	var code = $('#login').data('code')
		//	console.log(username,password,verity,code);
		//得到开关的状态
      /*  var state=$("#rememberPwd").prop("checked");
        alert(state);*/
       alert(vcode);
       alert(code);
       
		var uname = $('#uname').val();
		if(uname.length == 0) {
			layer.tips("用户名不能为空!", "#uname", {
        	tips: [2, "#FF0000"],
            time: 1000,
			});
				return;
		}
		
		var upwd = $('#upwd').val()
		if(upwd.length == 0) {
			layer.tips("密码不能为空!", "#upwd", {
				tips: [2, "#FF0000"],
        		time: 1000,
            });
		        return;
        }
		
		var patrn=/^(\w){6,12}$/
		if(!patrn.exec(upwd)){
			  layer.tips("密码应为6-12位！","#upwd",{
			    tips:2,
				time:1000
			});
				return;
		}
		
		if(vcode.length == 0) {
			layer.tips("验证码不能为空!", "#vcode", {
				tips: [2, "#FF0000"],
				time: 1000,
			});
		        return;
		}
		
        var patrn=/^[A-Za-z0-9]{4}$/
		if(!patrn.exec(vcode)){
			layer.tips("验证码长度不对或有非法字符！","#vcode",{
			    tips:2,
				time:1000
			});
			    return;
		}
		if(vcode.toLowerCase()!=code.toLowerCase()){
			layer.tips("验证码输入错误！","#vcode",{
				tips:1,
				time:1000
			});
				return;
		}
		
		$.ajax({
			url: "js/login.json",
			type: 'get',
			dataType: "json",
			success: function(result) {
				
				var userinfo = result.data;
				var temp = false;
				$.each(userinfo,function (login,value) {
					if(uname == value.uname && upwd == value.upwd){
						temp=true;
						return;
					}
				});	
				
				
				if(temp == false) 
					layer.msg("登陆失败！", {
						icon: 2,
						time: 2000
					});
				else {
					var state = $("#rememberPwd").prop("checked");
					if(state == true){
						localStorage.setItem("uname",uname);
						localStorage.setItem("upwd",upwd);
					}else{
						localStorage.clear();
					}
					sessionStorage.setItem("uname",uname);
					location.href = "manager.html";
				}
				
			},
			error: function() {
				layer.msg("请求服务器失败！", {
					icon: 2,
					time: 1000
				});

			}
		});
	});
			/*$("#register").click(function() {
				location.href = ''
			});*/
});

	/**生成一个随机数**/
			function randomNum(min, max) {
				return Math.floor(Math.random() * (max - min) + min);
			}
			/**生成一个随机色**/
			function randomColor(min, max) {
				var r = randomNum(min, max);
				var g = randomNum(min, max);
				var b = randomNum(min, max);
				return "rgb(" + r + "," + g + "," + b + ")";
			}
			
	/**绘制验证码图片**/
			function drawPic() {
				var canvas = document.getElementById("vcode_img");
				var width = canvas.width;
				var height = canvas.height;
				//获取该canvas的2D绘图环境 
				var ctx = canvas.getContext('2d');
				ctx.textBaseline = 'bottom';
				/**绘制背景色**/
				ctx.fillStyle = randomColor(180, 240);
				//颜色若太深可能导致看不清
				ctx.fillRect(0, 0, width, height);
				/**绘制文字**/
				var str = 'ABCEFGHJKLMNPQRSTWXY123456789';
				var code = "";
				//生成四个验证码
				for(var i = 1; i <= 4; i++) {
					var txt = str[randomNum(0, str.length)];
					code = code + txt;
					ctx.fillStyle = randomColor(50, 160);
					//随机生成字体颜色
					ctx.font = randomNum(15, 40) + 'px SimHei';
					//随机生成字体大小
					var x = 10 + i * 25;
					var y = randomNum(25, 35);
					var deg = randomNum(-45, 45);
					//修改坐标原点和旋转角度
					ctx.translate(x, y);
					ctx.rotate(deg * Math.PI / 180);
					ctx.fillText(txt, 0, 0);
					//恢复坐标原点和旋转角度
					ctx.rotate(-deg * Math.PI / 180);
					ctx.translate(-x, -y);
				}

				/**绘制干扰线**/
				for(var i = 0; i < 3; i++) {
					ctx.strokeStyle = randomColor(40, 180);
					ctx.beginPath();
					ctx.moveTo(randomNum(0, width / 2), randomNum(0, height / 2));
					ctx.lineTo(randomNum(0, width / 2), randomNum(0, height));
					ctx.stroke();
				}
				/**绘制干扰点**/
				for(var i = 0; i < 50; i++) {
					ctx.fillStyle = randomColor(255);
					ctx.beginPath();
					ctx.arc(randomNum(0, width), randomNum(0, height), 1, 0, 2 * Math.PI);
					ctx.fill();
				}
			//	$("#login").data('code', code);
				return code;
			}
